<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  <title>External map</title>
  <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css"
    crossorigin="anonymous">
  <link rel="stylesheet" type="text/css"
    href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.2/css/fontawesome.min.css"
    crossorigin="anonymous">
  <link rel="stylesheet" type="text/css"
    href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.2/css/solid.css" crossorigin="anonymous">
  <link rel="stylesheet" type="text/css"
    href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.2/css/brands.css" crossorigin="anonymous">
  <link rel="stylesheet" type="text/css" href="./theme/ol.css">
  <link rel="stylesheet" type="text/css" href="/theme/site.css">
  <link rel="icon" type="image/svg+xml" href="/theme/img/logo-light.svg" media="(prefers-color-scheme: light)" />
  <link rel="icon" type="image/svg+xml" href="/theme/img/logo-dark.svg" media="(prefers-color-scheme: dark)" />
</head>

<body>
  <header class="navbar navbar-expand-md navbar-dark mb-3 px-3 py-0 fixed-top" role="navigation">
    <a class="navbar-brand" href="/"><img src="/theme/img/logo-dark.svg" width="70" height="70"
        alt="">&nbsp;OpenLayers</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#olmenu"
      aria-controls="olmenu" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <!-- menu items that get hidden below 768px width -->
    <nav class="collapse navbar-collapse" id="olmenu">
      <ul class="nav navbar-nav ms-auto">
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="docdropdown" role="button" data-bs-toggle="dropdown"
            aria-haspopup="true" aria-expanded="false">Docs</a>
          <div class="dropdown-menu dropdown-menu-end mb-3" aria-labelledby="docdropdown">
            <a class="dropdown-item" href="/doc/">Docs</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="/doc/quickstart.html"><i class="fa fa-check fa-fw me-2 fa-lg"></i>快速入门</a>
            <a class="dropdown-item" href="/doc/faq.html"><i class="fa fa-question fa-fw me-2 fa-lg"></i>常见问题</a>
            <a class="dropdown-item" href="/doc/tutorials/"><i class="fa fa-book fa-fw me-2 fa-lg"></i>教程</a>

          </div>
        </li>
        <li class="nav-item"><a class="nav-link" href="/en/latest/examples/">Examples</a></li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="apidropdown" role="button" data-bs-toggle="dropdown"
            aria-haspopup="true" aria-expanded="false">
            <i class="fa fa-sitemap me-1"></i>API
          </a>
          <div class="dropdown-menu dropdown-menu-end mb-3" aria-labelledby="apidropdown">
            <a class="dropdown-item" href="/en/latest/apidoc/"><i class="fa fa-sitemap fa-fw me-2 fa-lg"></i>v8.2.0
              (latest)</a </div>
        </li>
      </ul>
    </nav>
  </header>

  <div class="container-fluid line-numbers">
    <div id="latest-check" class="alert alert-warning alert-dismissible" role="alert" style="display:none">
      <button id="latest-dismiss" type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
      This example uses OpenLayers v<span>8.2.0</span>. The <a id="latest-link" href="#" class="alert-link">latest</a>
      is v<span id="latest-version"></span>.
    </div>


    <div class="row-fluid">
      <a href="#" id="codepen-button" class="btn btn-link float-end">
        <i class="fab fa-codepen fa-lg"></i> Edit
      </a>
      <div class="span12">
        <h4 id="title">外部地图</h4>
        <p class="tags">
          <span class="badge-group">
            <a href="./index.html?q=external" class="badge badge-info">external</a><a
              class="badge badge-info tag-modal-toggle" data-bs-toggle="modal" data-bs-target="#tag-example-list"
              data-title="external" data-content="&lt;ul class=&quot;list-group&quot;&gt;
                  &lt;li&gt;&lt;a class=&quot;list-group-item list-group-item-action active&quot; href=&quot;./external-map.html&quot;&gt;External map&lt;/a&gt;
                  &lt;/ul&gt;" tabindex="0">1</a>
          </span>
          <span class="badge-group">
            <a href="./index.html?q=window" class="badge badge-info">window</a><a
              class="badge badge-info tag-modal-toggle" data-bs-toggle="modal" data-bs-target="#tag-example-list"
              data-title="window" data-content="&lt;ul class=&quot;list-group&quot;&gt;
                  &lt;li&gt;&lt;a class=&quot;list-group-item list-group-item-action active&quot; href=&quot;./external-map.html&quot;&gt;External map&lt;/a&gt;
                  &lt;/ul&gt;" tabindex="0">1</a>
          </span>
        </p>
        <div class="modal modal-tag-example" id="tag-example-list" tabindex="-1" role="dialog"
          aria-labelledby="tag-example-title" aria-hidden="true">
          <div class="modal-dialog modal-dialog-scrollable" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="tag-example-title"></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
              </div>
              <div class="modal-body">
                <div class="list-group"></div>
              </div>
            </div>
          </div>
        </div>
        <div id="map" class="map"></div>
        <input id="external-map-button" type="button" value="Open external map"></input>
        <span id="blocker-notice" hidden>Could not open map in external window. If you are using a popup or ad blocker
          you may need to disable it for this example.</span>

      </div>
      <form method="POST" id="codepen-form" target="_blank" action="https://codesandbox.io/api/v1/sandboxes/define">
        <input id="codesandbox-params" type="hidden" name="parameters">
      </form>
    </div>

    <div class="row-fluid">
      <div class="span12">
        <p id="shortdesc">Move a map to a separate window.</p>
        <div id="docs">
          <p>新开窗口加载地图。</p>
        </div>
      </div>
    </div>

    <div class="row-fluid">
      <h5 class="source-heading">main.js</h5>
      <pre><code id="example-js-source" class="language-js">import Map from &#x27;ol/Map.js&#x27;;
import OSM from &#x27;ol/source/OSM.js&#x27;;
import TileLayer from &#x27;ol/layer/Tile.js&#x27;;
import View from &#x27;ol/View.js&#x27;;
import {
  Control,
  FullScreen,
  defaults as defaultControls,
} from &#x27;ol/control.js&#x27;;
import {fromLonLat} from &#x27;ol/proj.js&#x27;;

class UnusableMask extends Control {
  constructor() {
    super({
      element: document.createElement(&#x27;div&#x27;),
    });
    this.element.setAttribute(&#x27;hidden&#x27;, &#x27;hidden&#x27;);
    this.element.className &#x3D; &#x27;ol-mask&#x27;;
    this.element.innerHTML &#x3D; &#x27;&lt;div&gt;Map not usable&lt;/div&gt;&#x27;;
  }
}

const localMapTarget &#x3D; document.getElementById(&#x27;map&#x27;);

const map &#x3D; new Map({
  target: localMapTarget,
  controls: defaultControls().extend([new FullScreen(), new UnusableMask()]),
  layers: [
    new TileLayer({
      source: new OSM(),
    }),
  ],
  view: new View({
    center: fromLonLat([37.41, 8.82]),
    zoom: 4,
  }),
});

let mapWindow;
function closeMapWindow() {
  if (mapWindow) {
    mapWindow.close();
    mapWindow &#x3D; undefined;
  }
}
// Close external window in case the main page is closed or reloaded
window.addEventListener(&#x27;pagehide&#x27;, closeMapWindow);

const button &#x3D; document.getElementById(&#x27;external-map-button&#x27;);

function resetMapTarget() {
  localMapTarget.style.height &#x3D; &#x27;&#x27;;
  map.setTarget(localMapTarget);
  button.disabled &#x3D; false;
}

function updateOverlay() {
  if (!mapWindow) {
    return;
  }
  const externalMapTarget &#x3D; mapWindow.document.getElementById(&#x27;map&#x27;);
  if (!externalMapTarget) {
    return;
  }
  if (document.visibilityState &#x3D;&#x3D;&#x3D; &#x27;visible&#x27;) {
    // Show controls and enable keyboard input
    externalMapTarget.classList.remove(&#x27;unusable&#x27;);
    externalMapTarget.setAttribute(&#x27;tabindex&#x27;, &#x27;0&#x27;);
    externalMapTarget.focus();
  } else {
    // Hide all controls and disable keyboard input
    externalMapTarget.removeAttribute(&#x27;tabindex&#x27;);
    externalMapTarget.classList.add(&#x27;unusable&#x27;);
  }
}
window.addEventListener(&#x27;visibilitychange&#x27;, updateOverlay);

button.addEventListener(&#x27;click&#x27;, function () {
  const blockerNotice &#x3D; document.getElementById(&#x27;blocker-notice&#x27;);
  blockerNotice.setAttribute(&#x27;hidden&#x27;, &#x27;hidden&#x27;);
  button.disabled &#x3D; true;

  // Reset button and map target in case window did not load or open
  let timeoutKey &#x3D; setTimeout(function () {
    closeMapWindow();
    resetMapTarget();
    blockerNotice.removeAttribute(&#x27;hidden&#x27;);
    timeoutKey &#x3D; undefined;
  }, 3000);

  mapWindow &#x3D; window.open(
    &#x27;resources/external-map-map.html&#x27;,
    &#x27;MapWindow&#x27;,
    &#x27;toolbar&#x3D;0,location&#x3D;0,menubar&#x3D;0,width&#x3D;800,height&#x3D;600&#x27;
  );
  mapWindow.addEventListener(&#x27;DOMContentLoaded&#x27;, function () {
    const externalMapTarget &#x3D; mapWindow.document.getElementById(&#x27;map&#x27;);
    localMapTarget.style.height &#x3D; &#x27;0px&#x27;;
    map.setTarget(externalMapTarget);

    if (timeoutKey) {
      timeoutKey &#x3D; clearTimeout(timeoutKey);
    }
    mapWindow.addEventListener(&#x27;pagehide&#x27;, function () {
      resetMapTarget();
      // Close window in case user does a page reload
      closeMapWindow();
    });

    updateOverlay();
  });
});
</code></pre>
    </div>

    <div class="row-fluid">
      <h5 class="source-heading">index.html</h5>
      <pre><code id="example-html-source" class="language-markup">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;External map&lt;/title&gt;
    &lt;link rel="stylesheet" href="node_modules/ol/ol.css"&gt;
    &lt;style&gt;
      .map {
        width: 100%;
        height: 400px;
      }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id&#x3D;&quot;map&quot; class&#x3D;&quot;map&quot;&gt;&lt;/div&gt;
    &lt;input id&#x3D;&quot;external-map-button&quot; type&#x3D;&quot;button&quot; value&#x3D;&quot;Open external map&quot;&gt;&lt;/input&gt;
    &lt;span id&#x3D;&quot;blocker-notice&quot; hidden&gt;Could not open map in external window. If you are using a popup or ad blocker you may need to disable it for this example.&lt;/span&gt;

    &lt;script type="module" src="main.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>
    </div>

    <div class="row-fluid extra-source">
      <h5 class="source-heading">resources/external-map-map.html</h5>
      <pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang&#x3D;&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;
    &lt;title&gt;Map&lt;/title&gt;
    &lt;link rel&#x3D;&quot;stylesheet&quot; type&#x3D;&quot;text/css&quot; href&#x3D;&quot;../theme/ol.css&quot;&gt;
    &lt;style&gt;
      body {
        margin: 0;
      }
      .map {
        height: 100vh;
        width: 100vw;
      }
      .map.unusable .ol-mask {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        user-select: none;
        background-color: rgba(0, 0, 0, .7);
        color: white;
        font: bold 3rem &#x27;Segoe UI&#x27;, Tahoma, Geneva, Verdana, sans-serif;
      }
      .map.unusable .ol-control {
        display: none;
      }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id&#x3D;&quot;map&quot; class&#x3D;&quot;map&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
    </div>
    <div class="row-fluid">
      <h5 class="source-heading">package.json</h5>
      <pre><code id="example-pkg-source" class="language-json">{
  &quot;name&quot;: &quot;external-map&quot;,
  &quot;dependencies&quot;: {
    &quot;ol&quot;: &quot;8.2.0&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;vite&quot;: &quot;^3.2.3&quot;
  },
  &quot;scripts&quot;: {
    &quot;start&quot;: &quot;vite&quot;,
    &quot;build&quot;: &quot;vite build&quot;
  }
}</code></pre>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js"></script>
  <script src="common.js"></script>
  <script src="external-map.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/components/prism-core.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/plugins/autoloader/prism-autoloader.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/plugins/toolbar/prism-toolbar.min.js"></script>
  <script
    src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.js"></script>
  <script src="./resources/common.js"></script>
  <script>
    document.getElementById('tag-example-list')
      .addEventListener('show.bs.modal', function (event) {
        const button = event.relatedTarget; // Button that triggered the modal
        this.querySelector('.modal-title').innerText = button.dataset.title;
        this.querySelector('.modal-body').innerHTML = button.dataset.content;
      });
  </script>
  <script>
    const releaseUrl = 'https://cdn.jsdelivr.net/npm/ol/package.json';
    fetch(releaseUrl).then(function (response) {
      return response.json();
    }).then(function (json) {
      const latestVersion = json.version;
      document.getElementById('latest-version').innerHTML = latestVersion;
      const url = window.location.href;
      const branchSearch = url.match(/\/([^\/]*)\/examples\//);
      const storageKey = 'ol-dismissed=-' + latestVersion;
      const dismissed = localStorage.getItem(storageKey) === 'true';
      if (branchSearch && !dismissed && /^v[0-9\.]*$/.test(branchSearch[1]) && '8.2.0' != latestVersion) {
        const link = url.replace(branchSearch[0], '/latest/examples/');
        fetch(link, { method: 'head' }).then(function (response) {
          const a = document.getElementById('latest-link');
          a.href = response.status == 200 ? link : '../../latest/examples/';
        });
        const latestCheck = document.getElementById('latest-check');
        latestCheck.style.display = '';
        document.getElementById('latest-dismiss').onclick = function () {
          latestCheck.style.display = 'none';
          localStorage.setItem(storageKey, 'true');
        }
      }
    });
  </script>
</body>

</html>